﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace WFWS.Core
{
    public interface IHttpRequestParser
    {
        int Parse(byte[] buffer, int offset, int length);

        RequestParserState CurrentState { get; set; }

        event EventHandler RequestCompleted;

        /// More body bytes have been received.
        event EventHandler<BodyEventArgs> BodyBytesReceived;

        /// Request line have been received.
        event EventHandler<RequestLineEventArgs> RequestLineReceived;

        /// A header have been received.
        event EventHandler<HeaderEventArgs> HeaderReceived;
    }

    public enum RequestParserState
    {
        /// <summary>
        /// Should parse the request line
        /// </summary>
        FirstLine,
        /// <summary>
        /// Searching for a complete header name
        /// </summary>
        HeaderName,
        /// <summary>
        /// Searching for colon after header name (ignoring white spaces)
        /// </summary>
        AfterName,
        /// <summary>
        /// Searching for start of header value (ignoring white spaces)
        /// </summary>
        Between,
        /// <summary>
        /// Searching for a complete header value (can span over multiple lines, as long as they are prefixed with one/more whitespaces)
        /// </summary>
        HeaderValue,

        /// <summary>
        /// Adding bytes to body
        /// </summary>
        Body
    }

    public class BodyEventArgs : EventArgs
    {
       
        public BodyEventArgs(byte[] buffer, int offset, int count)
        {

            Buffer = buffer;
            Offset = offset;
            Count = count;
        }

     
        public BodyEventArgs()
        {
        }

     
        public byte[] Buffer { get; set; }
     
        public int Count { get; set; }
    
        public int Offset { get; set; }
    }
    public class HeaderEventArgs : EventArgs
    {
        public HeaderEventArgs(string name, string value)
        {
            Name = name;
            Value = value;
        }

        public HeaderEventArgs() { }

        public string Name { get; set; }
        public string Value { get; set; }
    }
    public class RequestLineEventArgs : EventArgs
    {
        public RequestLineEventArgs(string httpMethod, string uriPath, string httpVersion)
        {
            HttpMethod = httpMethod;
            UriPath = uriPath;
            HttpVersion = httpVersion;
        }

        public RequestLineEventArgs() { }

        public string HttpMethod { get; set; }
        public string HttpVersion { get; set; }
        public string UriPath { get; set; }
    }
}
